package com.smile.weather2.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import com.google.gson.Gson;
import com.smile.weather2.R;
import com.smile.weather2.base.BaseActivity;
import com.smile.weather2.model.CityInfo;
import com.smile.weather2.model.WeatherInfo;
import com.smile.weather2.network.NetWork;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import retrofit2.Response;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class WeatherActivity extends BaseActivity {

    @InjectView(R.id.click)
    Button click;

    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        ButterKnife.inject(this);

        gson = new Gson();

    }

    @OnClick(R.id.click)
    public void onClick() {
        Subscription subscription = NetWork.getNetWorkApi()
//                .queryCity("allchina")
                .queryWeather("weather", "CN101010400")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<String>>() {
                    @Override
                    public void onCompleted() {
                        Log.i("wa", "oc--Completed!");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("wa", "oe--" + e.getMessage());
                    }

                    @Override
                    public void onNext(Response<String> response) {

                        StringBuilder builder = new StringBuilder(response.body());
                        builder.delete(0, builder.indexOf("[") + 1);
                        builder.delete(builder.lastIndexOf("]"), builder.length());

                        Log.i("wa", "on--" + builder.toString());

                        WeatherInfo weatherInfo = gson.fromJson(builder.toString(), WeatherInfo.class);
                        Log.i("wa", "on--" + weatherInfo.getBasic().toString());

//                        CityInfo cityInfo = gson.fromJson(response.body().toString(), CityInfo.class);
//                        Log.i("wa", "on--" + cityInfo.getCity_info().get(0).toString());

                    }
                });
    }
}
